package message;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import java.sql.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class InfoServlet extends HttpServlet {
	public void wrong3(){
		String msg="该用户尚未注册，登录失败！";
		int type=JOptionPane.YES_NO_CANCEL_OPTION;
		String title="信息提示";
		JOptionPane.showMessageDialog(null, msg, title, type);
	}
	public void wrong4(){
		String msg="用户密码不正确，登录失败！";
		int type=JOptionPane.YES_NO_CANCEL_OPTION;
		String title="信息提示";
		JOptionPane.showMessageDialog(null, msg, title, type);
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		String id=new String(request.getParameter("student_id").getBytes("ISO-8859-1"),"UTF-8");
		String sex=new String(request.getParameter("student_sex").getBytes("ISO-8859-1"),"UTF-8");
		String blood=new String(request.getParameter("student_blood").getBytes("ISO-8859-1"),"UTF-8");
		String address=new String(request.getParameter("student_address").getBytes("ISO-8859-1"),"UTF-8");
		String hobby=new String(request.getParameter("student_hobby").getBytes("ISO-8859-1"),"UTF-8");
		String password=new String(request.getParameter("student_password").getBytes("ISO-8859-1"),"UTF-8");
		String name=new String(request.getParameter("student_name").getBytes("ISO-8859-1"),"UTF-8");
		String className=new String(request.getParameter("student_className").getBytes("ISO-8859-1"),"UTF-8");
		try{
			Connection con=null;
			PreparedStatement stmt=null;
			Class.forName("com.mysql.jdbc.Driver");
			String url="jdbc:mysql://localhost:3306/bedroomallocation?useUnicode=true&characterEncoding=gbk";
			con=DriverManager.getConnection(url,"root","root");
			String sql="update student set sex=?"+",blood=?"+",address=?"+",hobby=?"+",message=?"+" where id='"+id+"'";
			stmt=con.prepareStatement(sql);
			stmt.setString(1, sex);
			stmt.setString(2, blood);
			stmt.setString(3, address);
			stmt.setString(4, hobby);
			stmt.setString(5, "30");
			stmt.execute();
			stmt.close();
			HttpSession session=request.getSession();
			ArrayList info=new ArrayList();
			LoginBean bean=new LoginBean();
			bean.setAccount(id);
			bean.setPassword(password);
			bean.setName(name);
			bean.setClassName(className);
			bean.setMessage("30");
			info.add(bean);
			if(request.getSession().getAttribute("login")!=null){
				request.getSession().removeAttribute("login");
				request.getSession().setAttribute("login",info);
			}
			session.setAttribute("login", info);
			response.sendRedirect("http://localhost:8888/BedroomAllocation/index.jsp");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		doGet(request,response);
	}
	
	public void init() throws ServletException {
		// Put your code here
	}
	
}
